package controllers;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import com.tinkerforge.AlreadyConnectedException;
import com.tinkerforge.IPConnection;
import com.tinkerforge.IPConnection.ConnectedListener;
import com.tinkerforge.IPConnection.EnumerateListener;
import com.tinkerforge.NotConnectedException;

import models.UserConfig;
import play.Logger;
import play.mvc.Controller;
import ch.bfh.abcm.rems.SensorControllerFactory;
import ch.bfh.abcm.rems.sensors.tinkerforge.TFSensorConfigDefinition;
import ch.bfh.abcm.rems.sensors.tinkerforge.TFSensorController;
import ch.bfh.abcm.rems.sensors.tinkerforge.TFSensorType;
import controllers.tinkerforge.REMSTFConfig;
import controllers.tinkerforge.REMSTFSensorDataPersister;

public class Application extends Controller {


    private static final int PORT = 4223;

    private static IPConnection connection;

    public static void index() {
        List<UserConfig> configs = UserConfig.findAll();

        render(configs);
    }

    public static void connecttoSensors() {
        Logger.debug("Index starttet...");
        REMSTFSensorDataPersister dataPersister = new REMSTFSensorDataPersister();
        REMSTFConfig config = new REMSTFConfig(dataPersister);
        TFSensorController sensorController = SensorControllerFactory.getTFSensorController(config);
        sensorController.connectToAvailableSensors();
        index();
    }

    public static void tinkerTest() {
        System.out.println("Tinkertest");
        
        try {
            createIPConn();
        } catch (UnknownHostException e) {
            System.out.println("UnknownHostException: " + e.getMessage());
        } catch (AlreadyConnectedException e) {
            System.out.println("AlreadyConnectedException: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("IOException: " + e.getMessage());
        }

        index();
    }

    public static void getAvailableHosts() throws UnknownHostException, IOException {
        checkHosts();
        
        index();
    }

    private static void checkHosts() throws UnknownHostException, IOException {
        InetAddress inet;

        System.out.println("****************"+ new Timestamp(new Date().getTime()));
        for (int i = 76; i < 86; i++) {
            inet = InetAddress.getByAddress(new byte[] { (byte) 192, (byte) 168, 1, (byte) i });
            if (inet.isReachable(2500)) {
                System.out.println("+++++++++++");
                System.out.println(inet.getHostAddress());
                System.out.println("+++++++++++");

            }
        }
        System.out.println("um 10 IP zu scannen ****"+ new Timestamp(new Date().getTime()));

    }

    private static void createIPConn() throws UnknownHostException, AlreadyConnectedException, IOException {
        if (connection == null) {
            System.out.println("IPConnection is null");
            connection = new IPConnection();
            connection.connect("192.168.1.77", 4223);

            connection.addConnectedListener(new ConnectedListener() {

                @Override
                public void connected(short arg0) {
                    System.out.println("ConnectedListener: " + arg0);

                }
            });

            connection.addEnumerateListener(new EnumerateListener() {

                @Override
                public void enumerate(String arg0, String arg1, char arg2, short[] arg3, short[] arg4, int arg5,
                        short arg6) {
                    System.out.println("*********** Enummerate *********************");
                    System.out.println("Arg 0 : " + arg0);
                    System.out.println("Arg 1 : " + arg1);
                    System.out.println("Arg 2 : " + arg2);
                    System.out.println("Arg 3 : " + arg3);
                    System.out.println("Arg 4 : " + arg4);
                    System.out.println("Arg 5 : " + arg5);
                    System.out.println("Arg 6 : " + arg6);
                    System.out.println("********************************");
                }
            });
        } else {
            System.out.println("IPConnection NOT Null, do Nothing");
        }
    }
}